/* _____________________________________________________________________________

	Project: The Effects of Independent Local Radio on Tanzanian Public Opinion: Evidence from a Planned Natural Experiment
	Authors: Donald P. Green, Dylan Groves, Constantine Manda, Beatrice Montano, Bardia Rahmani
	
	Purpose: Table A3 ingredients - Individual balance
	Date: 2022.10
________________________________________________________________________________*/

	
/* Introduction ________________________________________________________________*/
	
	clear all	
	set more off	
*	ssc install boottest
	
/* Set Control Directory _______________________________________________________*/

	*global user "X:/Dropbox/Wellspring Tanzania Papers/Wellspring Tanzania - Natural Experiment/pfm_ne_replication"
		
/* Load Data ___________________________________________________________________*/	
	
	use "${user}/pfm_ne_replication_data.dta", clear
	sort id_resp_uid
	set seed 1956
		
/* Setup _______________________________________________________________________*/


	/* Outcomes */
	#d ;
	local base_vars		resp_age resp_female resp_muslim b_resp_standard7 b_resp_married ///
						b_resp_religiosity b_resp_literate b_resp_lang_swahili ///
						b_resp_numhh b_resp_numkid b_resp_numolder b_resp_numyounger b_resp_numadult ///
						b_ge_index b_ge_raisekids b_ge_earning b_ge_school b_ge_leadership b_ge_noprefboy ///
						b_em_reject_story b_fm_reject ///
						b_ipv_rej_index b_ipv_norm_rej b_ipv_wifereact ///
						b_radio_any b_radio_community_dum b_radio_news_dum ///
						b_values_likechange b_values_money b_values_techgood ///
						b_values_elders b_values_respectauthority b_values_individualism ///
						b_radio_programs_music b_radio_programs_sport b_radio_programs_news b_radio_programs_romance b_radio_programs_social b_radio_programs_relig ///
						b_s3q2_freetime_rest b_s3q2_freetime_bar b_s3q2_freetime_sportwatch b_s3q2_freetime_sportplay ///
						b_s3q2_freetime_tv b_s3q2_freetime_cell b_s3q2_freetime_radio b_s3q2_freetime_community ///
						b_s3q3_converse_elec b_s3q3_converse_ipv b_s3q3_converse_discr b_s3q3_converse_vote b_s3q3_converse_water b_s3q3_converse_teachers ///
						b_asset_cell b_asset_tv b_asset_chair b_asset_sofa b_asset_table b_asset_motorcycle b_asset_radio_num ///
						b_freetime_media b_discuss_ptix b_discuss_nothing b_radio_news ///
						b_collact b_contactgov b_dk_ptix
						;
	#d cr
 
 
/* Define Matrix _______________________________________________________________*/
		
	/* Set Put Excel File Name */
	putexcel clear
	putexcel set "${user}/pfm_ne_replication_analysis_balance_rawoutput.xlsx", replace 
	
	putexcel A1 = ("variable")
	putexcel B1 = ("variablelabel")
	putexcel C1 = ("treatmean")
	putexcel D1 = ("treatsd")
	putexcel E1 = ("controlmean")
	putexcel F1 = ("controlsd")
	putexcel G1 = ("coef")
	putexcel H1 = ("pval")
	putexcel I1 = ("N")
	putexcel J1 = ("min")
	putexcel K1 = ("max")
	putexcel L1 = ("samplemean")
	putexcel M1 = ("controlN")
	putexcel N1 = ("treatN")
	putexcel O1 = ("boot_p")

	
/* Export Analysis ___________________________________________________________*/

	/* Set locals */
	local i = 1
	local row = 2

	/* Run and save for each variable */
	foreach dv of local base_vars  {

		/* Variable Name */
		qui ds `dv'
			global varname = "`r(varlist)'"  
			
		/* Variable Label */
		global varlabel : var label `dv'
			
		/* Treatment / Control Mean */
		sum `dv' if treat == 0 
			global control_mean `r(mean)'
			global control_sd `r(sd)'

		sum `dv' if treat == 1 
			global treat_mean `r(mean)'
			global treat_sd `r(sd)'
			
		sum `dv'
			global sample_mean `r(mean)'

		/* Variable Range */	
		sum `dv' 
			global min = r(min)
			global max = r(max)
			
		/* Count Treat and Control */
		tab `dv' if treat == 0
			global controlN = r(N)
			
		tab `dv' if treat == 1
			global treatN = r(N)

		/* Regression  */
		sort id_resp_uid
		set seed 1956
		xi: reg `dv' treat svy_replacement i.block_ne, cluster(id_village_uid)			
			matrix table = r(table)
			
		/* Wild Bootstrap */
		sort id_resp_uid
		set seed 1956
		boottest treat, nograph 
			global boot_p = r(p)
				
			/* Save values from regression */
			global coef = table[1,1]    	//beta
			global pval = table[4,1]		//pval
			global r2 	= `e(r2_a)' 		//r-squared
			global n 	= e(N) 				//N
			
			/* Put excel */
			putexcel A`row' = ("${varname}")
			putexcel B`row' = ("${varlabel}")
			putexcel C`row' = ("${treat_mean}")
			putexcel D`row' = ("${treat_sd}")
			putexcel E`row' = ("${control_mean}")
			putexcel F`row' = ("${control_sd}")
			putexcel G`row' = ("${coef}")
			putexcel H`row' = ("${pval}")
			putexcel I`row' = ("${n}")
			putexcel J`row' = ("${min}")
			putexcel K`row' = ("${max}")
			putexcel L`row' = ("${sample_mean}")
			putexcel M`row' = ("${controlN}")
			putexcel N`row' = ("${treatN}")
			putexcel O`row' = ("${boot_p}")
			
	/* Update locals */
	local row = `row' + 1
	local i = `i' + 1 
}	

